package com.ruoyi.entity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.entity.domain.Comic;
import com.ruoyi.entity.service.IComicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 漫画Controller
 *
 * @author Delamer
 * @date 2020-12-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/entity/comic")
public class ComicController extends BaseController {

    private final IComicService comicService;

    /**
     * 下载漫画
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        comicService.download(id);
        return AjaxResult.success("开始下载");
    }

    /**
     * 预览漫画
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        File file = new File("D://Data/Reden/Comic/" + id);
        File[] tempList = file.listFiles();
        String[] files = new String[tempList.length];
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                if (!tempList[i].getName().endsWith("tmp")) {
                    String name = tempList[i].getName();
                    int index = Integer.valueOf(name.substring(0, name.indexOf(".")));
                    files[index-1]=name;
                }
            }
            if (tempList[i].isDirectory()) {
            }
        }
        return AjaxResult.success(files);
    }

    /**
     * 查询漫画列表
     */
    @PreAuthorize("@ss.hasPermi('entity:comic:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comic comic) {
        startPage();
        LambdaQueryWrapper<Comic> lqw = new LambdaQueryWrapper<Comic>();

        List<Comic> list;
        if (comic.getComicCode() != null) {
            lqw.eq(Comic::getComicCode, comic.getComicCode());
        }
        if (StringUtils.isNotBlank(comic.getComicTitle())) {
            String[] params = comic.getComicTitle().split(" ");
            for (String param : params) {
                if (param.startsWith("!"))
                    lqw.notLike(Comic::getComicTitle, param.substring(1));
                else
                    lqw.like(Comic::getComicTitle, param);
            }
        }
        if (StringUtils.isNotBlank(comic.getComicRoman())) {
            String[] params = comic.getComicRoman().split(" ");
            for (String param : params) {
                if (param.startsWith("!"))
                    lqw.notLike(Comic::getComicRoman, param.substring(1));
                else
                    lqw.like(Comic::getComicRoman, param);
            }
        }
        if (StringUtils.isNotBlank(comic.getComicTags())) {
            String[] params = comic.getComicTags().split(" ");
            for (String param : params) {
                if (param.startsWith("!"))
                    lqw.notLike(Comic::getComicTags, param.substring(1));
                else
                    lqw.like(Comic::getComicTags, param);
            }
        }
        if (comic.getComicStatus() != null) {
            lqw.eq(Comic::getComicStatus, comic.getComicStatus());
        }
        list = comicService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出漫画列表
     */
    @PreAuthorize("@ss.hasPermi('entity:comic:export')")
    @Log(title = "漫画", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Comic comic) {
        LambdaQueryWrapper<Comic> lqw = new LambdaQueryWrapper<Comic>(comic);
        List<Comic> list = comicService.list(lqw);
        ExcelUtil<Comic> util = new ExcelUtil<Comic>(Comic.class);
        return util.exportExcel(list, "comic");
    }

    /**
     * 新增漫画
     */
    @PreAuthorize("@ss.hasPermi('entity:comic:add')")
    @Log(title = "漫画", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comic comic) {
        return toAjax(comicService.save(comic) ? 1 : 0);
    }

    /**
     * 修改漫画
     */
    @PreAuthorize("@ss.hasPermi('entity:comic:edit')")
    @Log(title = "漫画", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comic comic) {
        return toAjax(comicService.updateById(comic) ? 1 : 0);
    }

}
